﻿using Alos.Common;
using Alos.Entity;
using Alos.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace Alos.LanClass.Api
{
    /// <summary>
    /// 用户表
    /// </summary>
    public class UserInfoController : ApiController
    {
        ISysUserInfoService SysUserInfoService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_SysUserInfoService"></param>
        public UserInfoController(ISysUserInfoService _SysUserInfoService)
        {
            SysUserInfoService = _SysUserInfoService;
        }

        /// <summary>
        /// 获取当前用户信息（登陆后方可调用）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/userinfo/current")]
        public async Task<JsonRtn<SysUserInfo>> GetCurUser()
        {
            var jsRtn = new JsonRtn<SysUserInfo>();
            if (Common.Current.User == null)
            {
                jsRtn.state = RtnState.error;
                jsRtn.message = "用户未登陆!";
            }

            jsRtn.data = await Task.Factory.StartNew(uid =>
            {
                return SysUserInfoService.GetCurUser((int)uid);
            }, Common.Current.User.id);

            return jsRtn;
        }

        /// <summary>
        /// 根据班级编号获取学生信息
        /// </summary>
        /// <param name="id">班级编号</param>
        /// <param name="roleid">角色编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/userinfo/students")]
        public async Task<JsonRtn<List<SysUserInfo>>> students(int id)
        {
            var jsRtn = new JsonRtn<List<SysUserInfo>>();
            if (Common.Current.User == null)
            {
                jsRtn.state = RtnState.error;
                jsRtn.message = "用户未登陆!";
            }

            jsRtn.data = await Task.Factory.StartNew<List<SysUserInfo>>(cid =>
            {
                return SysUserInfoService.GetStudentByClassId((int)cid).ToList();
            }, id);

            return jsRtn;
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="linfo">登陆信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/userinfo/login")]
        public JsonRtn<dynamic> Login([FromBody]LoginInfo linfo)
        {
            var sui = SysUserInfoService.Login(linfo.username, linfo.password);
            string SessionKey = string.Empty;
            if (sui != null)
            {
                SessionKey = Common.Current.GetSessionKey(new AuthUser() { id = sui.ID, dt = (int)((DateTime.Now.DayOfYear) * 24 + DateTime.Now.Hour), ip = HttpContext.Current.Request.UserHostAddress });
            }
            return new JsonRtn<dynamic>() { state = sui != null && sui.ID > 0 ? RtnState.sucess : RtnState.fail, data = new { data = sui, sk = SessionKey }, message = sui != null && sui.ID > 0 ? "登陆成功！" : "用户名或密码失败！" };
        }
    }
}